#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<unistd.h>

#include "lvgl.h"
#include "lv_port_disp.h"
#include "lv_port_indev.h"

#include "netconnect.h"
#include "protocol.h"
#include "benchmark.h"
#include "gui_keyboard_lv.h"
#include "lv_tutorial_keyboard.h"


unsigned char testimgdat[800*480*2];
lv_img_dsc_t testimg = {
  .header.always_zero = 0,
  .header.w = 800,
  .header.h = 480,
  .data_size = 800*480*2,
  .header.cf = LV_IMG_CF_TRUE_COLOR,
  .data = testimgdat,
};

void testimginit(void)
{
    int i;
    for ( i = 0; i < (800*480); i++)
    {
        testimgdat[i*2] = 0xff;
        testimgdat[i*2+1] = 0xff;
    }
}


lv_style_t my_style_btn_release;//按钮释放状态
lv_style_t my_style_btn_press;//按钮按下状态
lv_style_t label_style24_w;//24大小白色
void typeinit(void)
{
    lv_style_copy(&my_style_btn_release,&lv_style_plain_color); //设置纯色的背景
    my_style_btn_release.body.main_color = LV_COLOR_MAKE(0xc1,0x27,0x2d); 
    my_style_btn_release.body.grad_color = my_style_btn_release.body.main_color;
    my_style_btn_release.body.opa = LV_OPA_COVER;//设置背景色完全不透明
    my_style_btn_release.body.radius = 5;//绘制圆角按钮
    my_style_btn_release.body.shadow.color = LV_COLOR_MAKE(0xc1,0x27,0x2d); //红
    my_style_btn_release.body.shadow.type = LV_SHADOW_FULL;//设置四边都有阴影
    my_style_btn_release.body.shadow.width = 2;//设置阴影的宽度
    my_style_btn_release.text.color = LV_COLOR_WHITE; 
    my_style_btn_release.body.padding.left = 5;//设置左内边距
    my_style_btn_release.body.padding.right = 5;//设置右内边距
    
    lv_style_copy(&my_style_btn_press,&lv_style_plain_color); //设置纯色的背景
    my_style_btn_press.body.main_color = LV_COLOR_MAKE(0xC9,0xC9,0xC9); 
    my_style_btn_press.body.grad_color = my_style_btn_press.body.main_color;
    my_style_btn_press.body.opa = LV_OPA_COVER;//设置背景色完全不透明
    my_style_btn_press.body.radius = 5;//绘制圆角按钮
    my_style_btn_press.body.shadow.color = LV_COLOR_MAKE(0xC9,0xC9,0xC9); 
    my_style_btn_press.body.shadow.type = LV_SHADOW_FULL;//设置四边都有阴影
    my_style_btn_press.body.shadow.width = 2;//设置阴影的宽度
    my_style_btn_press.text.color = LV_COLOR_WHITE; 
    my_style_btn_press.body.padding.left = 5;//设置左内边距
    my_style_btn_press.body.padding.right = 5;//设置右内边距

    /* 字体样式 */
    lv_style_copy(&label_style24_w,&lv_style_plain_color);//样式拷贝
    label_style24_w.text.font = &lv_font_roboto_16;//设置字体
    label_style24_w.text.color = LV_COLOR_WHITE;
}
lv_obj_t *          wp;
lv_obj_t *          btn_test;
lv_obj_t *          label_serialnub;


volatile unsigned char stringtmp[1024];
volatile unsigned char stringtmp2[1024];
gui_keyboard_t key_board;

static void btn_winswitch_cb(lv_obj_t * btn, lv_event_t event)
{
    
   
    if(event == LV_EVENT_RELEASED) {

        key_board.ptital = 0;
        key_board.pstr = (unsigned char *)stringtmp;
        key_board.pstrtmp = (unsigned char *)stringtmp2;
        key_board.strlen = 1024;
        key_board.psden = 1;
        key_board.kbtype = 1;
        stringtmp[0] = 0x00;
        stringtmp2[0] = 0x00;
        ui_getstring(&key_board);
    }
}

void test(void)
{
    wp = lv_img_create(lv_disp_get_scr_act(NULL), NULL);
    lv_img_set_src(wp, &testimg);
    lv_obj_set_pos(wp,0,0);

                            btn_test = lv_btn_create(wp, NULL);
    lv_obj_set_size(        btn_test,200,50);//
    lv_obj_set_pos(         btn_test,100,64);//设置坐标
    lv_btn_set_style(       btn_test,LV_BTN_STYLE_PR,&my_style_btn_press);
    lv_btn_set_style(       btn_test,LV_BTN_STYLE_REL,&my_style_btn_release);
    lv_btn_set_layout(      btn_test,LV_LAYOUT_CENTER);
    lv_obj_set_event_cb(    btn_test, btn_winswitch_cb);


                            label_serialnub = lv_label_create(btn_test, NULL);
    lv_label_set_text(      label_serialnub,"setting");
    lv_label_set_style(     label_serialnub,LV_LABEL_STYLE_MAIN,&label_style24_w);//给标签对象设置样式

}

// char  server_reply[2000];
// char  protocalbuf[1024];
int main() {
	
	struct sockaddr_in server;

    pthread_t  tsk_nc;

	
    // int i,ii;

    // int relen;

    // protocol_t protocol;

    // protocol.pbuf = protocalbuf;
    // protocol.pbufs = 0;
    // protocol.bufsize = 1024;
    // protocol.datacnt = 0;
    // protocol.step = 0;
    // protocol.targcnt = 0;
  

    /* lvgl */
    
    testimginit();
    lv_init();
    lv_port_disp_init();
    lv_port_indev_init();

    typeinit();
    
    //lv_label_set_style( btn1_label,LV_LABEL_STYLE_MAIN,&label_style24_w);//给标签对象设置样式
    
    // lv_obj_set_user_data(btn1,(lv_obj_user_data_t)&wjwt_time);
    // lv_obj_set_event_cb(btn1, btn_winswitch_cb);

	// 
	socket_desc = socket(AF_INET, SOCK_STREAM, 0);
	if (-1 == socket_desc) {
		perror("connot create socket");
		exit(1);
	}
	
	//server.sin_addr.s_addr = inet_addr("192.168.88.250");
    server.sin_addr.s_addr = inet_addr("192.168.9.39");
    //server.sin_addr.s_addr = inet_addr("169.254.27.42");
	server.sin_family = AF_INET;
	server.sin_port = htons(8888);
	
	//
	if (connect(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0) {
		perror("connot connect");
		return 1;
	}

    if(pthread_create(&tsk_nc,NULL,(void *)task_netconnect,NULL) !=0)
    {
        return -1;
    }

    
    test();
    //lv_tutorial_keyboard(NULL);
    while (1)
    {
        lv_task_handler();//lvgl的事务处理
        // if (send(socket_desc, message, 16, 0) < 0) {
        //     perror("send data error");
        //     return 2;
        // }
    
        // printf("send message success\n");

        // if(message)
        // {
        //     free(message);
        // }


        // relen = recv(socket_desc, server_reply, 1000, 0);
        // if (relen < 0) 
        // {
        //     perror("recv error");
        //     //return 3;
        // }
        // else
        // {
        //     for ( i = 0; i < relen; i++)
        //     {
        //         if(ProtocolRun(&protocol,server_reply[i]))
        //         {
                    
        //             if(protocol.pbuf[5] == 0x01)
        //             {
        //                 touchnet.tf = 0;
        //                 touchnet.type = 1;
        //                 ncmemcp((unsigned char *)&touchnet.tx,&protocol.pbuf[6],4);
        //                 ncmemcp((unsigned char *)&touchnet.ty,&protocol.pbuf[10],4);
        //                 touchnet.tf = 1;
        //                 printf("recive data[%d](%d,%d)(%d)!!!\r\n",relen,touchnet.tx,touchnet.ty,ii++);
        //             }

        //             if(protocol.pbuf[5] == 0x02)
        //             {
        //                 touchnet.tf = 0;
        //                 touchnet.type = 0;
        //                 ncmemcp((unsigned char *)&touchnet.tx,&protocol.pbuf[6],4);
        //                 ncmemcp((unsigned char *)&touchnet.ty,&protocol.pbuf[10],4);
        //                 touchnet.tf = 1;
        //                 printf("recive data[%d](%d,%d)(%d)!!!\r\n",relen,touchnet.tx,touchnet.ty,ii++);
        //             }

        //             if(protocol.pbuf[5] == 0x03)
        //             {
        //                 lv_tick_inc(20);
        //                 //printf("tick %d\r\n",ii++);
        //             }

        //         }
        //     }
        // }

        
    }


	
	// �ر�socket
	close(socket_desc);
	return 0;
}